import {Component, ElementRef, EventEmitter, HostBinding, HostListener, Input, OnInit, Output, ViewEncapsulation} from '@angular/core';
import {FormComponent} from "../form.component";
import {isInclude} from "../../../../../support/element";
import {Field} from "../../../field/field.model";
import {Cell} from "../form.model";

@Component({
  selector: 'ss-platform-dynamic-form-cell',
  templateUrl: './cell.component.html',
  styleUrls: ['./cell.component.scss'],
  host: {class: "ss-platform-dynamic-form-cell", "[class.current]": "cell&&cell.isEdit", "[id]": "cell.field.fieldId"},
  encapsulation: ViewEncapsulation.None
})
export class CellComponent implements OnInit {

  @Input("ss-row")
  row: Array<Array<any>>;
  @Input("ss-cell")
  cell: Cell;

  element: HTMLElement;
  rowElement: HTMLElement;

  constructor(private elementRef: ElementRef, public formComponent: FormComponent) {
  }

  ngOnInit(): void {
    this.element = this.elementRef.nativeElement;
    this.rowElement = this.element.parentElement;
  }

  @HostListener("click")
  click(): void {
    this.formComponent.cellComponents.forEach(component => {
      component.cell.isEdit = false;
    });
    this.cell.isEdit = !this.cell.isEdit;
    this.cell.isEdit && this.formComponent.onSelectCell.emit(this.cell);
  }

  include(x, y): boolean {
    return isInclude(x, y, this.element);
  }
}
